package com.zecan.blog.security.handler;

import lombok.NoArgsConstructor;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @ClassName PasswordEncoder
 * @Description
 * @date 2022/10/31 10:38
 * @Version 1.0
 */
@NoArgsConstructor
public class PasswordEncoder extends BCryptPasswordEncoder {


    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {

        // 获取前端的rsa加密后的密码数据
        String pwd = rawPassword.toString();

       /* try {
            // 使用BCryptPasswordEncoder对用户传递过来的密码间加密
           // pwd = new BCryptPasswordEncoder().encode(pwd);
        } catch (Exception e) {
            throw new BadCredentialsException(e.getMessage());
        }*/
        // security中 进行密码匹配是通过原始密码传入 内部自己进行加密后比较 , 所以不用给 pwd 再次加密
        if(encodedPassword != null  && encodedPassword.length() != 0) {
            // 使用BCrypt 加密进行比较
            return new BCryptPasswordEncoder().matches(pwd,encodedPassword);
        }
        return false;
    }
}
